package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.exception.BusinessException;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void save(Employee employee, Long[] ids) {
        //调mapper接口中的方法
        employeeMapper.insert(employee);
        //维护中间表的关系
        if (ids!=null){
            for(Long roleId:ids){
                employeeMapper.insertRelation(employee.getId(),roleId);
            }

        }
    }

    @Override
    @Transactional
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
        employeeMapper.deleteRelation(id);
    }

    @Override
    @Transactional
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        //删除关系
        employeeMapper.deleteRelation(employee.getId());
        //插入新的关系
        if (ids!=null){
            for(Long roleId:ids){
                employeeMapper.insertRelation(employee.getId(),roleId);
            }

        }
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listAll() {

        return employeeMapper.selectAll();
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        //定义查询页,每页页数
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> list = employeeMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public Employee empLogin(String username, String password) {
        //校验
        if(StringUtils.isEmpty(username)){
            throw new BusinessException("账号不能为空");
        }
        if(StringUtils.isEmpty(password)){
            throw new BusinessException("密码不能为空");
        }
        Employee emp = employeeMapper.selectByUsername(username);

        if(emp==null||!emp.getPassword().equals(password)){
            throw new BusinessException("账号密码有误");
        }

        //记录登录日志等（很多）
        return emp;
    }

    @Override
    public void exportXls(ServletOutputStream outputStream) throws IOException {

        //定义（创建）一个工作表
        Workbook wb = new HSSFWorkbook();
        //创建工作页
        Sheet sheet1 = wb.createSheet("员工通讯录");
        //定义行
        Row row = sheet1.createRow(0);
        //定义Excel中的第一行信息（头信息）
       row.createCell(0).setCellValue("员工编号");
       row.createCell(1).setCellValue("员工姓名");
       row.createCell(2).setCellValue("员工邮箱");

        List<Employee> employees = employeeMapper.selectAll();
        for (int i=0;i<employees.size();i++){
            Employee emp = employees.get(i);
            row = sheet1.createRow(i+1);
            row.createCell(0).setCellValue(emp.getId());
            row.createCell(1).setCellValue(emp.getName());
            row.createCell(2).setCellValue(emp.getEmail());

        }
        //将文件输出到浏览器中
        wb.write(outputStream);

    }

    @Override
    public void importXls(MultipartFile file) throws IOException {
        Workbook wb = new HSSFWorkbook(file.getInputStream());
        Sheet sheet = wb.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i= 1;i <= lastRowNum;i++){
            Row row = sheet.getRow(i);
            Employee employee = new Employee();
            employee.setUsername(row.getCell(0).getStringCellValue());
            employee.setName(row.getCell(1).getStringCellValue());
            Double numericCellValue = row.getCell(2).getNumericCellValue();
            employee.setAge(numericCellValue.intValue());
            employee.setEmail(row.getCell(3).getStringCellValue());
            employee.setAdmin(false);
            employee.setPassword("888888");
            employeeMapper.insert(employee);
        }

    }

}
